package com.meida.boot.handler.app;

import com.meida.common.enums.AuthStatusEnum;
import com.meida.common.mybatis.entity.EntityMap;
import com.meida.common.mybatis.interceptor.GetInterceptor;
import com.meida.common.mybatis.query.CriteriaQuery;
import com.meida.common.security.OpenHelper;
import com.meida.common.utils.FlymeUtils;
import com.meida.module.product.client.entity.ProdShop;
import com.meida.module.system.client.entity.SysCompany;
import com.meida.module.system.provider.service.SysCompanyService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Optional;

/**
 * 商家店铺完善资料
 *
 * @author zyf
 */
@Component("shopPerfectDetailsHandler")
@Log4j2
public class ShopPerfectDetailsHandler implements GetInterceptor {


    @Autowired
    private SysCompanyService companyService;

    /**
     * 查询条件扩展
     *
     * @param cq
     * @return
     */
    @Override
    public void prepare(CriteriaQuery cq, EntityMap params) {
        Long companyId = OpenHelper.getCompanyId();
        cq.eq(ProdShop.class, "companyId", companyId);
    }


    /**
     * 返回结果集扩展
     *
     * @param cq
     * @return
     */
    @Override
    public void complete(CriteriaQuery cq, EntityMap map) {
        Long companyId = OpenHelper.getCompanyId();
        if (FlymeUtils.isNotEmpty(companyId)) {
            SysCompany company = companyService.getById(companyId);

            Integer audiState = FlymeUtils.getInteger(company.getAuditState(), 1);
            map.put("audiState", audiState);
            if (FlymeUtils.isNotEmpty(company)) {
                if (audiState.equals(AuthStatusEnum.AUTHFAIL.getCode())) {
                    map.put("authInfo", "失败原因:" + Optional.ofNullable(company.getAuditContent()).orElse(""));
                }
                if (audiState.equals(AuthStatusEnum.AUTHSUCCESS.getCode())) {
                    map.put("adminUrl", "http://admin.hswlm.cn");
                }
                map.put("companyName", company.getCompanyName());
            }
            if (audiState.equals(AuthStatusEnum.AUTHING.getCode())) {
                map.put("authInfo", "您提交的申请正在审核中,我们工作人员会在2个工作日内给您回复,请耐心等待");
            }
        } else {
            map.put("audiState", 0);
            map.put("authInfo", "请提交认证信息");
        }
    }
}
